package com.sgu6.sgu6_machine.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.sgu6.middleware.util.DateUtils;
import com.sgu6.sgu6_machine.dto.ChargeDto;
import com.sgu6.sgu6_machine.dto.EletricDto;
import com.sgu6.sgu6_machine.dto.ListGoodsSimpleDto;
import com.sgu6.sgu6_machine.dto.MachineConfigDto;
import com.sgu6.sgu6_machine.dto.PowerDto;
import com.sgu6.sgu6_machine.dto.VoltageDto;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class DBManager {
    private final static String dbName = "Sgu6_Machine_DB";
    private static DBManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private GreenDaoContext mContext;
    private DaoSession daoSession;

    public DBManager(Context context) {
        mContext = new GreenDaoContext(context);
        openHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        daoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }


    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

//    /**
//     * 插入一条记录
//     *
//     * @param machine 机器信息
//     */
//    public void insertMachine(Machine machine) throws Exception {
//        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
//        DaoSession daoSession = daoMaster.newSession();
//        MachineDao machineDao = daoSession.getMachineDao();
//        machineDao.insert(machine);
//    }
//
//
//    /**
//     * 删除一条记录
//     *
//     * @param machine
//     */
//    public void deleteMachine(Machine machine) throws Exception{
//        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
//        DaoSession daoSession = daoMaster.newSession();
//        MachineDao machineDao = daoSession.getMachineDao();
//        machineDao.delete(machine);
//    }
//
//
//    /**
//     * 查询用户列表
//     */
//    public List<Machine> queryMachineList() throws Exception {
//        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
//        DaoSession daoSession = daoMaster.newSession();
//        MachineDao machineDao = daoSession.getMachineDao();
//        QueryBuilder<Machine> qb = machineDao.queryBuilder();
//        List<Machine> list = qb.list();
//        return list;
//    }
//
//
//    /**
//     * 更新一条记录
//     *
//     * @param machine
//     */
//    public void updateMachine(Machine machine) throws Exception{
//        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
//        DaoSession daoSession = daoMaster.newSession();
//        MachineDao machineDao = daoSession.getMachineDao();
//        machineDao.update(machine);
//    }


    /**
     * 插入一条记录
     *
     * @param machineConfigDto 机器配置信息
     */
    public void insertMachineConfig(MachineConfigDto machineConfigDto) throws Exception {
        MachineConfigDtoDao machineInfoDao = daoSession.getMachineConfigDtoDao();
        machineInfoDao.insert(machineConfigDto);
    }


    /**
     * 删除一条记录
     *
     * @param machineInfo 机器配置信息
     */
    public void deleteMachineInfo(MachineConfigDto machineInfo) throws Exception {
        MachineConfigDtoDao machineInfoDao = daoSession.getMachineConfigDtoDao();
        machineInfoDao.delete(machineInfo);
    }


    /**
     * 查询用户列表
     */
    public List<MachineConfigDto> queryMachineInfoList() throws Exception {
        MachineConfigDtoDao machineInfoDao = daoSession.getMachineConfigDtoDao();
        QueryBuilder<MachineConfigDto> qb = machineInfoDao.queryBuilder();
        List<MachineConfigDto> list = qb.list();
        return list;
    }


    /**
     * 更新一条记录
     *
     * @param machineInfo 机器配置信息
     */
    public void updateMachineInfo(MachineConfigDto machineInfo) throws Exception {
        MachineConfigDtoDao machineInfoDao = daoSession.getMachineConfigDtoDao();
        machineInfoDao.update(machineInfo);
    }


    /////////////////////

    /**
     * 插入一条记录
     *
     * @param listGoodsSimpleDto
     */
    public void insertMachineGoods(ListGoodsSimpleDto listGoodsSimpleDto) throws Exception {
        ListGoodsSimpleDtoDao goodsDao = daoSession.getListGoodsSimpleDtoDao();
        goodsDao.insert(listGoodsSimpleDto);
    }


    /**
     * 删除一条记录
     *
     * @param listGoodsSimpleDto
     */
    public void deleteMachineGoods(ListGoodsSimpleDto listGoodsSimpleDto) throws Exception {
        ListGoodsSimpleDtoDao goodsDao = daoSession.getListGoodsSimpleDtoDao();
        goodsDao.delete(listGoodsSimpleDto);
    }

    public void deleteAllMachineGoods() throws Exception {
        ListGoodsSimpleDtoDao goodsDao = daoSession.getListGoodsSimpleDtoDao();
        goodsDao.deleteAll();
    }


    /**
     * 查询用户列表
     */
    public List<ListGoodsSimpleDto> queryMachineGoods() throws Exception {
        ListGoodsSimpleDtoDao goodsDao = daoSession.getListGoodsSimpleDtoDao();
        QueryBuilder<ListGoodsSimpleDto> qb = goodsDao.queryBuilder();
        List<ListGoodsSimpleDto> list = qb.list();
        return list;
    }


    /**
     * 更新一条记录
     *
     * @param listGoodsSimpleDto
     */
    public void updateMachineGoods(ListGoodsSimpleDto listGoodsSimpleDto) throws Exception {
        ListGoodsSimpleDtoDao goodsDao = daoSession.getListGoodsSimpleDtoDao();
        List<ListGoodsSimpleDto> listGoodsSimpleDtos = queryMachineGoods();
        if (listGoodsSimpleDtos.size() == 0) {
            goodsDao.insert(listGoodsSimpleDto);
        } else {
            goodsDao.deleteAll();
            goodsDao.insert(listGoodsSimpleDto);
        }
    }


    /**
     * 插入电压
     *
     * @param listGoodsSimpleDto
     */
    public void insertVoltage(VoltageDto voltageDto) throws Exception {
        VoltageDtoDao voltageDtoDao = daoSession.getVoltageDtoDao();
        voltageDtoDao.insert(voltageDto);
    }

    public void clearVoltage() throws Exception {
        String day = DateUtils.getPreDay();
        VoltageDtoDao voltageDtoDao = daoSession.getVoltageDtoDao();
        voltageDtoDao.queryBuilder().where(VoltageDtoDao.Properties.Date.lt(day)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public List<VoltageDto> queryVoltage() throws Exception {
        VoltageDtoDao voltageDtoDao = daoSession.getVoltageDtoDao();
        QueryBuilder<VoltageDto> qb = voltageDtoDao.queryBuilder();
        List<VoltageDto> list = qb.list();
        return list;
    }



    /**
     * 插入电流
     *
     * @param listGoodsSimpleDto
     */
    public void insertEletric(EletricDto eletricDto) throws Exception {
        EletricDtoDao eletricDtoDao = daoSession.getEletricDtoDao();
        eletricDtoDao.insert(eletricDto);
    }

    public void clearEletric() throws Exception {
        String day = DateUtils.getPreDay();
        EletricDtoDao eletricDtoDao = daoSession.getEletricDtoDao();
        eletricDtoDao.queryBuilder().where(EletricDtoDao.Properties.Date.lt(day)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public List<EletricDto> queryEletric() throws Exception {
        EletricDtoDao eletricDtoDao = daoSession.getEletricDtoDao();
        QueryBuilder<EletricDto> qb = eletricDtoDao.queryBuilder();
        List<EletricDto> list = qb.list();
        return list;
    }


    /**
     * 插入功率
     *
     * @param listGoodsSimpleDto
     */
    public void insertPower(PowerDto powerDto) throws Exception {
        PowerDtoDao powerDtoDao = daoSession.getPowerDtoDao();
        powerDtoDao.insert(powerDto);
    }

    public void clearPower() throws Exception {
        String day = DateUtils.getPreDay();
        PowerDtoDao powerDtoDao = daoSession.getPowerDtoDao();
        powerDtoDao.queryBuilder().where(PowerDtoDao.Properties.Date.lt(day)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public List<PowerDto> queryPower() throws Exception {
        PowerDtoDao powerDtoDao = daoSession.getPowerDtoDao();
        QueryBuilder<PowerDto> qb = powerDtoDao.queryBuilder();
        List<PowerDto> list = qb.list();
        return list;
    }

    /**
     * 插入电量
     *
     * @param listGoodsSimpleDto
     */
    public void insertCharge(ChargeDto chargeDto) throws Exception {
        ChargeDtoDao chargeDtoDao = daoSession.getChargeDtoDao();
        chargeDtoDao.insert(chargeDto);
    }

    public void clearCharge() throws Exception {
        String day = DateUtils.getPreDay();
        ChargeDtoDao chargeDtoDao = daoSession.getChargeDtoDao();
        chargeDtoDao.queryBuilder().where(ChargeDtoDao.Properties.Date.lt(day)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public List<ChargeDto> queryCharge() throws Exception {
        ChargeDtoDao chargeDtoDao = daoSession.getChargeDtoDao();
        QueryBuilder<ChargeDto> qb = chargeDtoDao.queryBuilder();
        List<ChargeDto> list = qb.list();
        return list;
    }

}